package com.lin.androidgame21;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

/***

 *Create By LWL Write

 **/

class Sprite {
    private Bitmap bitmap;
    private int width;
    private int height;
    private int x;
    private int y;
    private int speedX;
    private int speedY;
    private boolean isVisible;
    private int frameNumber;
    private int[] frameX;
    private int[] frameY;
    private Rect src;
    private Rect dst;
    private int frameSequenceIndex;
    private int[] frameSequence;

    Sprite(Bitmap bitmap) {
        this(bitmap, bitmap.getWidth(), bitmap.getHeight());
    }

    Sprite(Bitmap bitmap, int width, int height) {
        this.height = height;
        this.width = width;
        this.bitmap = bitmap;
        int w = bitmap.getWidth() / width;
        int h = bitmap.getHeight() / height;
        frameNumber = w * h;
        frameX = new int[frameNumber];
        frameY = new int[frameNumber];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                frameX[w * i + j] = width * j;
                frameY[w * i + j] = height * i;
            }
        }
        src = new Rect();
        dst = new Rect();
        frameSequence = new int[frameNumber];
        for (int i = 0; i < frameNumber; i++) {
            frameSequence[i] = i;
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSpeedX() {
        return speedX;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public int getSpeedY() {
        return speedY;
    }

    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }

    public boolean isVisible() {
        return isVisible;
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }

    public int getFrameNumber() {
        return frameNumber;
    }

    public void setFrameNumber(int frameNumber) {
        this.frameNumber = frameNumber;
    }

    public int getFrameSequenceIndex() {
        return frameSequenceIndex;
    }

    public void setFrameSequenceIndex(int frameSequenceIndex) {
        this.frameSequenceIndex = frameSequenceIndex;
    }

    public int[] getFrameSequence() {
        return frameSequence;
    }

    public void setFrameSequence(int[] frameSequence) {
        this.frameSequence = frameSequence;
    }

    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;

    }

    public void logic() {
        move(speedX, speedY);

    }

    public void outOfBounds() {
        if (getX() < 0 || getX() > 1280 || getY() < 0 || getY() > 768) {
            setVisible(false);
        }
    }

    public void draw(Canvas canvas) {
        if (isVisible()) {
            src.set(frameX[frameSequence[frameSequenceIndex]],
                    frameY[frameSequence[frameSequenceIndex]],
                    frameX[frameSequence[frameSequenceIndex]] + width,
                    frameY[frameSequence[frameSequenceIndex]] + height);
            dst.set(x, y, x + width, y + height);
            canvas.drawBitmap(bitmap, src, dst, null);
        }
    }

    public void move(float distanceX, float distanceY) {
        x += distanceX;
        y += distanceY;
        outOfBounds();
    }

    public boolean collisionWith(Sprite sprite) {
        if (!isVisible() || !sprite.isVisible()) {
            return false;
        }
        if (getX() < sprite.getX() && getX() + getWidth() < sprite.getX()) {
            return false;
        }
        if (sprite.getX() < getX() && sprite.getX() + sprite.getWidth() < getX()) {
            return false;
        }
        if (getY() < sprite.getY() && getY() + getHeight() < sprite.getY()) {
            return false;
        }
        if (sprite.getY() < getY() && sprite.getY() + sprite.getHeight() < getY()) {
            return false;
        }
        return true;
    }

    public void nextFrame() {
        setFrameSequenceIndex((getFrameSequenceIndex() + 1) % getFrameSequence().length);
    }
}
